<?php
/**
 * Created by PhpStorm.
 * User: liubao
 * Date: 2018/8/29
 * Time: 10:32
 */

namespace Qywx\Controller;


use Home\Controller\CrmProjectController;
use Home\Services\CrmProjectsService;
use Home\Services\UserService;

class IndexController extends BaseController
{
    public function index($action = '/Qywx/Index/back')
    {
        if (isset($_SESSION['user'])) {
            $this->wxindex();
        } else {
            return BaseController::index($action);
        }
    }

    public function back()
    {
        $userInfo = BaseController::back();
        if (isset($userInfo)) {
            $name = $userInfo['name'];
            $user = M("db_user")->where("username='$name'")->find();
            if ($user) {
                session('user', $user);
                //授权成功！
                $this->redirect('Index/wxindex');
            } else {
                $this->error('暂未匹配到登录人信息，请联系EHR人员');
            }

        } else {
            $this->error('授权失败');
        }
    }

    public function wxindex()
    {
        echo 123;
        die;
    }

    //定时查询到期收款企业微信推送
    public function stage_date()
    {
        $db_contract_back = M("db_contract_back");
        $where['back_status'] = array('neq', 1);
        $where['del'] = array('eq', 1);
        $where1['stage_date'] = array('exp', "= date_add(CURDATE(),interval 1 month)");
        $where2['stage_date'] = array('exp', "= date_add(CURDATE(),interval 15 day)");
        $where3['stage_date'] = array('exp', "= date_sub(CURDATE(),interval 15 day)");
        $where4['stage_date'] = array('exp', "= date_sub(CURDATE(),interval 2 month)");
        $res_countdown1 = $db_contract_back->where($where)->where($where1)->select();//提前一个月
        $res_countdown2 = $db_contract_back->where($where)->where($where2)->select();//提前15天
        $res_countdown3 = $db_contract_back->where($where)->where($where3)->select();//延期15天
        $res_countdown4 = $db_contract_back->where($where)->where($where4)->select();//延期60天
        $this->push_text_back($res_countdown1, 1);
        $this->push_text_back($res_countdown2, 2);
        $this->push_text_back($res_countdown3, 3);
        $this->push_text_back($res_countdown4, 4);
    }

    public function push_card_message($action_tag, $title, $description, $url)
    {
        $db_push_notification = M("db_push_notification");
        $res_push = $db_push_notification->where("open='1' and action_tag='$action_tag'")->find();
        if (!empty($res_push)) {
            $touser = str_replace(',', '|', $res_push['push_user_id']);
            $toparty = str_replace(',', '|', $res_push['push_department_id']);
        } else {
            exit();
        }
        $date = date('Y-m-d H:i:s');
        $description = "<div class=\"gray\">$date</div> <div class=\"normal\">{$description['stage']}</div><div class=\"normal\">{$description['leader']}</div><div class=\"highlight\"></div>";
        return parent::push_card($touser, $toparty, $title, $description, $url);
    }

    //收款计划到期提醒
    public function push_text_back($res_countdown, $type)
    {
        if (!empty($res_countdown)) {
            $db_push_notification = M("db_push_notification");
            $res_project = CrmProjectController::getCrmProjectArray($this->array_column($res_countdown, 'project_id'));
            switch ($type) {
                case 1:
                    $action_tag = 'countdown-30';
                    $message_fix = '还有一个月到期';
                    break;
                case 2;
                    $action_tag = 'countdown-15';
                    $message_fix = '还有15天到期';
                    break;
                case 3;
                    $action_tag = 'countdown+15';
                    $message_fix = '已经逾期15天未处理';
                    break;
                case 4;
                    $action_tag = 'countdown+60';
                    $message_fix = '已经逾期60天未处理';
                    break;
                default;
                    $action_tag = 'countdown-15';
                    $message_fix = '还有15天到期';
                    break;
            }
            $where['action_tag'] = array('eq', $action_tag);
            $where['open'] = array('eq', '1');
            $res_push = $db_push_notification->where($where)->find();
            if (!empty($res_push)) {
                $touser = str_replace(',', '|', $res_push['push_user_id']);
                $toparty = str_replace(',', '|', $res_push['push_department_id']);
                foreach ($res_project as $value) {
                    foreach ($res_countdown as $item) {
                        if ($value['project_id'] == $item['project_id']) {
                            $where_projects['project_id'] = $value['project_id'];
                            $projects_leader = CrmProjectsService::getCrmProjectsByField('leader', $where_projects);//运营项目负责人
                            if (!empty($projects_leader)) {//如果是运营项目也通知给运营项目负责人
                                $projects_leader = UserService::getUserByField('spell', $projects_leader);
                                $touser = $touser . '|' . $projects_leader;
                            }
                            $message = '您有一个项目：' . $value['name'] . '--第' . $item['stage'] . '阶段的收款' . $message_fix;
                            $this->push_message($touser, $toparty, $message);
                        }
                    }
                }
            }
        }
    }

    function array_column(array &$rows, $column_key, $index_key = null) {
        $data = array();
        if (empty($index_key)) {
            foreach($rows as $row) {
                $data[] = $row[$column_key];
            }
        }else{
            foreach($rows as $row){
                $data[$row[$index_key]] = $row[$column_key];
            }
        }
        return $data;
    }

}